package com.bwie.controller;

import com.bwie.bean.Brand;
import com.bwie.bean.Goods;
import com.bwie.service.MyService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MyController {
    @Resource
    MyService myService;

    @RequestMapping("list")
    public String list(
            Model model,
            @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum
    ){
        HashMap map = new HashMap();
        PageHelper.startPage(pageNum,4);
        List list = myService.list(map);
        PageInfo page = new PageInfo(list);
        model.addAttribute("map",map);
        model.addAttribute("page",page);
        return "list";
    }
    @RequestMapping("add")
    @ResponseBody
    public Object add(Goods goods, MultipartFile photo, HttpSession session){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            goods.setSpic(upload);
        }
        int i = myService.add(goods);
        return i;
    }
    @RequestMapping("toadd")
    public String toadd(){
        return "add";
    }
    @RequestMapping("del")
//    @ResponseBody
    public Object del(String sid){
        int i = myService.del(sid);
        return "redirect:list";
    }
    @RequestMapping("update")
    @ResponseBody
    public Object update(Goods goods,MultipartFile photo,HttpSession session){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            goods.setSpic(upload);
        }
        int i = myService.update(goods);
        return i;
    }
    @RequestMapping("toupdate")
    public String toupdate(){
        return "update";
    }

    @RequestMapping("hx")
    @ResponseBody
    public Map hx(Integer sid){
        Map hx = myService.hx(sid);
        return hx;
    }
    @RequestMapping("brandList")
    @ResponseBody
    public List<Brand> brandList(){
        List<Brand> list = myService.brandList();
        return list;
    }
}
